<?php
/**
 * ===============================================
 * 版权所有 2019-2050 青岛铁锤科技有限公司，并保留所有权利。
 * 网站地址: http://www.huaxianling.com
 * ===============================================
 * Author: Happy dong
 * Created Time: 2019-06-28 16:23
 * Modified Time: 2019-06-28 16:23
 * Version: 2.0
 */

namespace App\Modules\Order\Criterias;


use Flower\Core\Repository\Contracts\CriteriaInterface;
use Flower\Core\Repository\Contracts\RepositoryInterface;
use function foo\func;

class searchOrderWxCriteria implements CriteriaInterface
{
	protected $where;

	public function __construct($memberId,$status,$orderNo)
	{
		$this->where =[
			'member_id' => $memberId,
			'status'    => $status,
			'order_no'  => $orderNo
		];
	}

	public function apply($model, RepositoryInterface $repository)
	{
		$map = [
			'member_id' => $this->where['member_id']
		];
		if(isset($this->where['order_no'])){
			$map['order_no'] = $this->where['order_no'];
		}

		$isShowComment = ($this->where['status'] == config('constant.orderStatus.done')) ? true  : false;

		$whereIn = isset($this->where['status']) ?  ($this->where['status'] == config('constant.orderStatus.done') ? [config('constant.orderStatus.done'),config('constant.orderStatus.comment')] : [$this->where['status']] ) : [config('constant.orderStatus.confirm')];

		 $list = $model->with('user:id,nick,portrait,mobile','items:id,order_id,service_id,service_name,price,count','store:id,name,address,doorplate')
			->where($map)
			->when($isShowComment,function($query){
				$query->with(['comment'=>function($v){
					$v->select(['id','order_id','member_id','user_id','comment','is_goods','created_at'])->with(['member:id,name,face']);
				}]);
			})
			->whereIn('status',$whereIn)
			->distinct('id');
		  return $this->where['status'] == config('constant.orderStatus.done') ? $list->orderBy('finish_time','desc'): $list->orderBy('id','desc');
	}

}